'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 副本
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var soldierAttr = require('./soldierAttr');
var buildAttr = require('./buildAttr');
var buildType = require('../../consts/buildType');
var heroConf = require('../../config/heroConf');
var buildTypeUtil = require('../../util/buildTypeUtil');
var dataApi = require('../../libs/dataApi');
var constantUtil = require('../../util/constantUtil');
var skillAttr = require('./skillAttr');
var buildService = require('../../services/buildService.js');

/**
 * 获取副本数据
 * @param  {[type]} copy_id [description]
 * @return {[type]}         [description]
 */
exports.getData = function () {
    // 英雄数据
    var guide_hero_conf = dataApi.guide_hero.findById(4);

    // 建筑数据
    var guide_build_conf = dataApi.guide_war.findById(1);

    return {
        name: _getName(),
        lv: guide_hero_conf.hero_lvl,
        hero: _getHeroAttr(guide_hero_conf),
        road_list: _getRoadList(guide_build_conf),
        build_list: _getBuildList(guide_build_conf),
        soldier_list: _getSoldierList(guide_build_conf)
    };
};

/**
 * 获取名字
 * @param  {[type]} copy_id [description]
 * @return {[type]}         [description]
 */
var _getName = function () {
    return '';
};

/**
 * 获取英雄数据
 */
var _getHeroAttr = function (guide_hero_conf) {
    // 英雄类型数据
    var hero_type_conf = heroConf.getHeroTypeConf(guide_hero_conf.hero_type);

    return {
        // 英雄职业
        hero_type: guide_hero_conf.hero_type,

        // 属性
        attr: {
            // 最小攻击力
            min_atk: guide_hero_conf.atk || 0,

            // 最大攻击力
            max_atk: guide_hero_conf.atk || 0,

            // 血量
            hp: guide_hero_conf.hp || 0,

            // 命中率
            hit: 100,

            // 闪避率
            dodge: 0,

            // 攻击间隔
            atk_interval: hero_type_conf.atk_interval || 0,

            // 攻击范围
            atk_range: hero_type_conf.atk_range || 0,

            // 警界范围
            warn_range: hero_type_conf.warn_range || 0,

            // 速度
            speed: hero_type_conf.speed || 0
        },
        equip_list: _getEquipAttr(guide_hero_conf),
        skill_list: _getSkillAttr(guide_hero_conf),
    };
};

/**
 * 获取装备数据
 * @return {[type]} [description]
 */
var _getEquipAttr = function (guide_hero_conf) {
    var equip_conf_list = guide_hero_conf.equipment.split(',');
    var equip_list = [];
    _.each(equip_conf_list, function (equip_conf) {
        equip_list.push(+equip_conf);
    });
    return equip_list;
};

/**
 * 获取技能数据
 * @return {[type]} [description]
 */
var _getSkillAttr = function (guide_hero_conf) {
    var skill_data = [];
    var skill_all_conf = guide_hero_conf.skill.split(',');
    _.each(skill_all_conf, function (skill_conf_item) {
        var skill_conf = skill_conf_item.split(':');
        skill_data.push(skillAttr.getAttr(skill_conf[0], skill_conf[1]));
    });

    return skill_data;
};

/**
 * 获取道路
 */
var _getRoadList = function (guide_build_all_conf) {
    var road_list = [];
    _.each(guide_build_all_conf, function (guide_build_conf) {
        if (guide_build_conf.building_id == buildType.ROAD) {
            road_list.push(guide_build_conf.coordinate);
        }
    });
    return road_list;
};

/**
 * 获取建筑列表
 * @param  {[type]} copy_id [description]
 * @return {[type]}         [description]
 */
var _getBuildList = function (guide_build_all_conf) {
    var build_list = [];
    _.each(guide_build_all_conf, function (guide_build_conf) {
        if (guide_build_conf.building_id != buildType.ROAD) {
            // 建筑的金币比例
            build_list.push(_getBuild(guide_build_conf, 0));
        }
    });
    return build_list;
};

/**
 * 获取士兵列表
 * @param  {[type]} copy_id       [description]
 * @return {[type]}               [description]
 */
var _getSoldierList = function (guide_build_all_conf) {
    var soldier_list = [];
    var soldier_type_lv_list = {};
    _.each(guide_build_all_conf, function (guide_build_conf) {
        var soldier_info = guide_build_conf.soldier_info;
        if (soldier_info.length <= 0) {
            return;
        }
        var soldier_conf_list = soldier_info.split(',');
        _.each(soldier_conf_list, function (soldier_conf) {
            var soldier_type = parseInt(soldier_conf.split(':')[0]);
            var soldier_lv = parseInt(soldier_conf.split(':')[1]);
            var soldier_star = parseInt(soldier_conf.split(':')[2]);
            soldier_type_lv_list[soldier_type] = {
                soldier_lv: soldier_lv,
                soldier_star: soldier_star
            };
        });
    });

    _.each(soldier_type_lv_list, function (soldier_lv_conf, soldier_type) {
        var attr = soldierAttr.getAttr(
            soldier_type,
            soldier_lv_conf.soldier_lv,
            soldier_lv_conf.soldier_star
        );
        soldier_list.push(attr);
    });
    return soldier_list;
};

/**
 * 获取建筑的item
 * @param  {[type]} build_conf [description]
 * @return {[type]}            [description]
 */
var _getBuild = function (guide_build_conf) {
    var build_id = guide_build_conf.building_id;
    var build_lv = guide_build_conf.building_lvl;
    var build_cell = guide_build_conf.coordinate;
    var build_star = guide_build_conf.building_star;
    var build_type = buildTypeUtil.getBuildType(build_id);

    var build_item = {
        build_id: build_id,
        cell: build_cell,
        per: 0,
        star: build_star,
        attr: buildAttr.getAttr(build_type, build_lv)
    };

    // 附加星级加成
    buildService.getBuildStarEff(build_item);

    if (build_type == buildType.WALL) {
        build_item.soldier_list = _getBuildSoldierList(guide_build_conf.soldier_info);
    }

    return build_item;
};

/**
 * 获取城墙的兵种数据
 * @return {Array} [description]
 */
var _getBuildSoldierList = function (wall_soldier_info) {
    var smpl_soldier_list = [];

    var wall_soldier_list = _parseWallSoldierList(wall_soldier_info);
    _.forEach(wall_soldier_list, function (batch_soldier_list) {
        var batch_info = [];
        _.forEach(batch_soldier_list, function (soldier_item) {
            batch_info.push(soldier_item.soldier_type);
        });

        smpl_soldier_list.push({
            single_soldier_list: batch_info
        });
    });
    return smpl_soldier_list;
};

/**
 * 获取城墙的兵种数据
 * @return {Array} [description]
 */
var _parseWallSoldierList = function (wall_soldier_info) {
    var wall_soldier_list = [];
    if (!wall_soldier_info || wall_soldier_info.length <= 0) {
        return wall_soldier_list;
    }
    var batch_info_arr = wall_soldier_info.split(';');
    _.forEach(batch_info_arr, function (batch_info) {
        wall_soldier_list.push(_parseSoldierList(batch_info));
    });
    return wall_soldier_list;
};

/**
 * 获取批次的兵种数据
 * @return {Array} [description]
 */
var _parseSoldierList = function (batch_soldier_info) {
    var soldier_list = [];
    if (batch_soldier_info.length <= 0) {
        return soldier_list;
    }

    var soldier_info_list = batch_soldier_info.split(',');
    _.forEach(soldier_info_list, function (soldier_info_item) {
        var soldier_info = soldier_info_item.split(':');
        var soldier_type = parseInt(soldier_info[0]);
        var soldier_lv = parseInt(soldier_info[1]);
        var soldier_star = parseInt(soldier_info[2]);

        soldier_list.push({
            soldier_type: soldier_type,
            soldier_lv: soldier_lv,
            soldier_star: soldier_star
        });
    });
    return soldier_list;
};
